class UserStationController < ApplicationController
  def book
    session[:user_course_id] = params[:course_id]
    session[:user_registration_course_id] = params[:registration_course_id]
    redirect_to :controller=>"auto_user_stations"
  end

  def station_calendar
    if params[:id]
      @station = Station.find(params[:id])
      session[:station_id] = @station.id
    else
      if session[:station_id]
        @station = Station.find(session[:station_id])
      else
        @station = nil
      end
    end
    @month = nil
    @year = nil
    if params[:month]
      @month = "#{params[:month]}".to_i
    end
    if params[:year]
      @year = "#{params[:year]}".to_i
    end
    if !@month
      @month = Date.today.month
    end
    if !@year
      @year = Date.today.year
    end
    session[:c_month] = @month
    session[:c_year] = @year
    render :action=>"station_calendar", :layout=>"main"
  end

  def previous_year
    redirect_to :action=>"station_calendar", :month=>session[:c_month], :year=>session[:c_year]-1
  end

  def previous_month
    if params[:c_month]==1
      redirect_to :action=>"station_calendar", :month=>12, :year=>session[:c_year]-1
    else
      redirect_to :action=>"station_calendar", :month=>session[:c_month]-1, :year=>session[:c_year]
    end
  end

  def next_month
    if params[:c_month]==12
      redirect_to :action=>"station_calendar", :month=>1, :year=>session[:c_year]+1
    else
      redirect_to :action=>"station_calendar", :month=>session[:c_month]+1, :year=>session[:c_year]
    end
  end

  def next_year
    redirect_to :action=>"station_calendar", :month=>session[:c_month], :year=>session[:c_year]+1
  end

  def day_slot
    @station = Station.find(session[:station_id])
    @book_date = Date.parse("#{params[:id]}")
    @slots = StationSlot.find(:all, :conditions=>["book_date=? AND station_id=?", @book_date, session[:station_id]], :order=>"start_time, end_time, serial")
    render :action=>"day_slot", :layout=>"main"
  end

  def book_slot
    slot = StationSlot.find(:first, :conditions=>["gencode=?", params[:id]])
    if slot.s_status=='Free'
      old_slots = StationSlot.find(:all, :conditions=>["course_id=? AND user_id=?", slot.course_id, session[:user_id]])
      old_slots.each do |os|
        os.s_status = 'Free'
        os.user_id = nil
        os.registration_course_id = nil
        os.save
      end
      slot.s_status = 'Booked'
      slot.user_id = session[:user_id]
      slot.registration_course_id = session[:user_registration_course_id]
      slot.save
    end
    redirect_to :action=>"day_slot", :id=>slot.book_date
  end

  def cancel_slot
    slot = StationSlot.find(:first, :conditions=>["gencode=?", params[:id]])
    slot.s_status = 'Free'
    slot.registration_course_id = nil
    slot.user_id = nil
    slot.save
    redirect_to :action=>"day_slot", :id=>slot.book_date
  end

  def print_book
    @slot = StationSlot.find(:first, :conditions=>["gencode=?", params[:id]])
    @course = Course.find(@slot.course_id)
    @station = Station.find(@slot.station_id)
    @user = User.find(@slot.user_id)
    render :action=>"print_book", :layout=>"main_no"
  end

  def check
    render :action=>"check", :layout=>"main"
  end

  def do_check
    user = User.find(session[:user_id])
    if user.role.include? 'T'
      @slot = StationSlot.find(:first, :conditions=>["serial=?", params[:serial]])
      render :action=>"do_check", :layout=>"main"
    else
      render :text=>"You have no permission!", :layout=>"main"
    end
  end

  def save_score
    user = User.find(session[:user_id])
    if user.role.include? 'T'
      slot = StationSlot.find(:first, :conditions=>["gencode=?", params[:id]])
      slot.score = params[:score].to_f
      slot.save
      rc = RegistrationCourse.find(slot.registration_course_id)
      rc.practise = params[:score].to_f
      rc.save
      redirect_to :action=>"do_check", :serial=>slot.serial
    else
      render :text=>"You have no permission!", :layout=>"main"
    end
  end
end
